import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const routes = [
  {
    name: '/',
    component: () => import('@/view/index'),
    meta: {
      title: '首页'
    }
  },
  {
    name: 'user',
    component: () => import('@/view/user/user'),
    meta: {
      title: '会员中心'
    }
  },
  {
    name: 'cart',
    component: () => import('@/view/cart'),
    meta: {
      title: '购物车'
    }
  },
  {
    name: 'qrScan',
    component: () => import('@/view/qrcode/qrcode'),
    meta: {
      title: '扫码'
    }
  },
  {
    name: 'goods',
    component: () => import('@/view/goods'),
    meta: {
      title: '商品详情'
    }
  }
]

// add route path
routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '')
})

const router = new Router({ routes })

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title
  if (title) {
    document.title = title
  }
  next()
})
router.afterEach(route => {
  window.scroll(0, 0);
});

export {
  router
}
